package com.xframe.xdal.core.serialize.lang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xframe.xdal.core.serialize.JsonModel;
import com.xframe.xdal.core.serialize.lang.staticlist.StaticListItem;
import com.xframe.xdal.core.serialize.lang.staticlist.StaticListLanguageItem;
import com.xframe.xdal.core.util.FileUtil;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 功能描述:语言环境上下文对象
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-28
 */
@Data
@Log4j2
public class LanguageEnvironmentContext {

    static final String FILE_NAME = "model.json";
    static final String STATIC_LIST_FILE_NAME = "staticList.json";

    static LanguageEnvironmentContext instance;
    public static LanguageEnvironmentContext getInstance(){
        return instance;
    }

    private Map<String, LanguageItem> map = new HashMap<>();

    private Map<String, StaticListLanguageItem> staticListItemMap = new HashMap<>();
    /**
     * 功能描述:私有构造函数
     *
     */
    private LanguageEnvironmentContext(){

    }

    public static void init(String basePath){
        instance = new LanguageEnvironmentContext();
        Map<String, LanguageItem> languageItemMap = procFiles(basePath);
        Map<String, StaticListLanguageItem> staticListLanguageItemHashMap = procStaticFile(basePath);
        instance.map = languageItemMap;
        instance.staticListItemMap = staticListLanguageItemHashMap;
    }

    public String getModelAttrText(String language,String modelCode,String attrName,String headText){
        LanguageItem languageItem = map.get(language);
        if(languageItem == null){
            return headText;
        }
        return languageItem.getAttrText(modelCode,attrName,headText);
    }

    public String getModelName(String language,String modelCode){
        LanguageItem languageItem = map.get(language);
        if(languageItem == null){
            return modelCode;
        }
        return languageItem.getModelName(modelCode);
    }

    /**
     * 获得对应语言的静态列表的list值
     * @param language 语言
     * @param attrName 字段名称
     * @param value 字段值
     * @return 翻译后的值
     */
    public Object getStaticText(String language,String attrName,Object value){
        StaticListLanguageItem languageItem = staticListItemMap.get(language);
        if(languageItem == null){
            return value;
        }
        return languageItem.getListItem(attrName,value);
    }

    private static Map<String, LanguageItem> procFiles(String basePath){
        Map<String, LanguageItem> languageItemMap = new HashMap<>();
        List<String> langList = FileUtil.readDirectoryName(basePath);
        for(String langDir : langList){
            LanguageItem languageItem = new LanguageItem();
            String fullPath = basePath + langDir + File.separator + FILE_NAME;
            Map map = FileUtil.readFileGetJson(fullPath);
            Set<Map.Entry<String,Object>> ent =  map.entrySet();
            for(Map.Entry<String,Object> entry:ent){
                String mapKey = entry.getKey();
                ModelLanguageItem modelLanguageItem = JSON.parseObject(entry.getValue().toString(), ModelLanguageItem.class);
                languageItem.add(mapKey,modelLanguageItem);
            }
            languageItemMap.put(langDir,languageItem);
        }
        return languageItemMap;
    }

    private static Map<String,StaticListLanguageItem> procStaticFile(String basePath){
        Map<String, StaticListLanguageItem> staticListLanguageItemHashMap = new HashMap<>();
        List<String> langList = FileUtil.readDirectoryName(basePath);
        for(String langDir : langList){
            StaticListLanguageItem staticListLanguageItem = new StaticListLanguageItem();
            String fullPath = basePath + langDir + File.separator + STATIC_LIST_FILE_NAME;
            Map map = FileUtil.readFileGetJson(fullPath);
            Set<Map.Entry<String,Object>> ent =  map.entrySet();
            for(Map.Entry<String,Object> entry:ent){
                String mapKey = entry.getKey();
                StaticListItem listItem = JSON.parseObject(entry.getValue().toString(), StaticListItem.class);
                staticListLanguageItem.add(mapKey,listItem);
            }
            staticListLanguageItemHashMap.put(langDir,staticListLanguageItem);
        }
        return staticListLanguageItemHashMap;
    }
}
